var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();

Page({
  data: {
    openSelectHerb:false,
    selectHerbDone: false,
    tempSelectHerb:null,
    herbItemList: [],//可选的药品列表
    herbList:[],//初始、已选择的列表
    total: 0
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    if(options.herbListStr){
      this.setData({
        herbList:JSON.parse(options.herbListStr)
      });
    }
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
    this.getHerbItemList();
  },
  getHerbItemList() {
    let that = this;
    util.request(api.HerbSelectList).then(function(res) {
      if (res.errno === 0) {
        console.log(res.data.list)
        that.setData({
          herbItemList: res.data.list
        });
      }
    });
  },
  herbAddOrUpdate(e){
    
  },
  //选择药品品类
  chooseHerb(){
    let that = this;
    this.setData({
      openSelectHerb: !this.data.openSelectHerb
    });
  },
  selectHerb(event){
    let that = this;
    let herbIndex = event.target.dataset.herbIndex;
    let herbItem = that.data.herbItemList[herbIndex];

    let herbItemList = that.data.herbItemList.map(item => {
      //标记已选择的
     if (herbItem.code == item.code) {
        item.selected = true;
      } else {
        item.selected = false;
      }
      return item;
    })

    this.setData({
      herbItemList: herbItemList,
      tempSelectHerb:herbItem,
      selectHerbDone:true
    })
  },
  updateParentHerbListData(){
    let that = this;
    let pages = getCurrentPages();
    let beforePage = pages[pages.length - 2]
    beforePage.setData({
      herbList:that.data.herbList,
    })
  },
  doneSelectHerb(){
    if (this.data.selectHerbDone === false) {
      return false;
    }
    let that = this;

    let targetItem = that.data.herbList.find((t)=>t.code == that.data.tempSelectHerb.code);
    if(targetItem){
      wx.showModal({
        title: '错误信息',
        content: '当前品类已选择',
        showCancel: false
      });
      return false;
    }
    that.data.herbList.push(that.data.tempSelectHerb);
    this.updateParentHerbListData();
    this.resetSelectStatus();
    this.setData({
      herbList:that.data.herbList,
      openSelectHerb: false,
      tempSelectHerb:null
    });
  },
  //重置选中状态
  resetSelectStatus(){
    let herbItemList = this.data.herbItemList.map(item => {
      item.selected = false;
      return item;
    })
    this.setData({
      herbItemList: herbItemList
    });
  },
  cancelSelectHerb(){
    this.setData({
      openSelectHerb: false
    });
  },
  deleteHerb(event) {
    // console.log(event.target)
    let that = this;
    wx.showModal({
      title: '',
      content: '确定要删除品类？',
      success: function(res) {
        if (res.confirm) {
          let herbId = event.target.dataset.herbId;
          that.data.herbList.splice(that.data.herbList.findIndex( (t) => t.id === herbId), 1)
          that.updateParentHerbListData();
          that.setData({
            herbList: that.data.herbList
          })
        }
      }
    })
    return false;
  },
  
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  }
})